package cc.rengu.igas.momp.core.service.trans;

import cc.rengu.igas.momp.common.constant.CommonConstant;
import cc.rengu.igas.momp.common.dao.StatisticsStatementInfoMapper;
import cc.rengu.igas.momp.common.dao.impl.StatisticsStatementInfoMapperImpl;
import cc.rengu.igas.momp.common.entity.Organization;
import cc.rengu.igas.momp.common.entity.TMStat2C;
import cc.rengu.igas.momp.common.entity.TMStat2CDay;
import cc.rengu.igas.momp.common.entity.StatisticsStatementQueryRespInfo;
import cc.rengu.igas.momp.common.enums.McTypeEnum;
import cc.rengu.igas.momp.common.enums.RespCodeEnum;
import cc.rengu.igas.momp.facade.bean.BankToClientMcStatInfoBean;
import cc.rengu.igas.momp.facade.bean.BankToClientMcTxnStatInfoBean;
import cc.rengu.igas.momp.facade.request.QueryBankToClientMcStatRequest;
import cc.rengu.igas.momp.facade.response.QueryBankToClientMcStatResponse;
import cc.rengu.oltp.service.base.RadpService;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.base.StringUtil;
import com.alibaba.fastjson.JSON;

import java.io.ByteArrayOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author : liujinan
 * @version 1.0.0
 * @date 2020/4/1
 * @Description: 银行对客类营销活动统计报表查询功能实现
 */
public class BankToClientMcStatQueryService extends RadpService {
    //营销活动编号
    String mcNo;
    //营销活动类型
    String mcType;
    //营销活动表json字符串
    String initStatData;

    @Override
    protected int bizInit(String s) {
        return 0;
    }

    @Override
    protected int bizProcess(String s, int i) {

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();

        try {
            QueryBankToClientMcStatRequest queryBankToClientMcStatRequest = new QueryBankToClientMcStatRequest();
            ConvertUtil.convertOutput(queryBankToClientMcStatRequest);
            QueryBankToClientMcStatResponse queryBankToClientMcStatResponse = new QueryBankToClientMcStatResponse();
            queryBankToClientMcStatResponse.setHeader(queryBankToClientMcStatRequest.getHeader());
            BizResponse<QueryBankToClientMcStatResponse> bizResponse = new BizResponse<>();
            bizResponse.setResult(queryBankToClientMcStatResponse);

            // 服务调用
            BizResponse<QueryBankToClientMcStatResponse> bizResponseNew = bankToClientMcStatQuery(xmlTreeUtil, queryBankToClientMcStatRequest, queryBankToClientMcStatResponse);

            ConvertUtil.convertInput(bizResponseNew.getResult());

        } catch (BizException e) {
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, e.getCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, e.getMessage());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e1));
            }
        } catch (Exception e) {
            rglog.error(StringUtil.ExceptionToString(e));
            try {
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_CODE, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespCode());
                xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.RESP_DESC, OltpRpcdEnum.OLTP_PROCESS_ERROR.getRespDesc());
                xmlTreeUtil.setXmlTreeStringValue(CommonConstant.KEY_RESPDESC, e.getMessage());
            } catch (Exception e1) {
                rglog.error(StringUtil.ExceptionToString(e));
            }
        }

        return CommonConstant.PROCESS_SUCCESS;
    }

    @Override
    protected int bizTerminate(String s) {
        return 0;
    }

    private BizResponse<QueryBankToClientMcStatResponse> bankToClientMcStatQuery(XmlTreeUtil xmlTreeUtil, QueryBankToClientMcStatRequest queryBankToClientMcStatRequest, QueryBankToClientMcStatResponse queryBankToClientMcStatResponse) throws Exception {

        BizResponse<QueryBankToClientMcStatResponse> bizResponse = new BizResponse<>();

        String queryClause = queryBankToClientMcStatRequest.getHeader().getInstId() + queryBankToClientMcStatRequest.getMcNo() + queryBankToClientMcStatRequest.getMcName() +
                queryBankToClientMcStatRequest.getMcType() + queryBankToClientMcStatRequest.getMcStatus() + queryBankToClientMcStatRequest.getMcOrgId();

        //营销活动信息List
        List<StatisticsStatementQueryRespInfo> mcMoneyOffNOList = new ArrayList<>();
        List<StatisticsStatementQueryRespInfo> mcCouponNOList = new ArrayList<>();
        //判断上否上送营销活动编号
        if (!StringUtil.isNullorEmpty(queryBankToClientMcStatRequest.getMcNo())) {
            mcNo = queryBankToClientMcStatRequest.getMcNo();
            mcType = mcNo.substring(2, 4);
            if (mcType.equals(McTypeEnum.COUPON_ALL.getMcTypeCode()) || mcType.equals(McTypeEnum.COUPON_FREE.getMcTypeCode())) {
                //查询卡券类营销活动信息
                getMcCouponListInfo(queryBankToClientMcStatRequest, mcCouponNOList);
            } else if (mcType.equals(McTypeEnum.REDUCTION_FIXED_AMOUNT.getMcTypeCode()) ||
                    mcType.equals(McTypeEnum.REDUCTION_RANDOM_AMOUNT.getMcTypeCode()) ||
                    mcType.equals(McTypeEnum.REDUCTION_FIXED_RATE.getMcTypeCode()) ||
                    mcType.equals(McTypeEnum.REDUCTION_RANDOM_RATE.getMcTypeCode())) {
                //查询减免类营销活动信息
                getMcMoneyOffListInfo(queryBankToClientMcStatRequest, mcMoneyOffNOList);
            }
        } else {
            //查询减免类营销活动信息
            getMcMoneyOffListInfo(queryBankToClientMcStatRequest, mcMoneyOffNOList);
            //查询卡券类营销活动信息
            getMcCouponListInfo(queryBankToClientMcStatRequest, mcCouponNOList);
        }

        if (mcMoneyOffNOList.isEmpty() && mcCouponNOList.isEmpty()) {
            rglog.error("根据查询条件<{}>查询到0条营销活动信息!", queryClause);
            queryBankToClientMcStatResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
            queryBankToClientMcStatResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);
        } else {
            List<StatisticsStatementQueryRespInfo> mcInfoList = new ArrayList<>();
            //将减免类营销活动添加到mcNOList
            mcInfoList.addAll(mcMoneyOffNOList);
            //将卡券类营销活动添加到mcNOList
            mcInfoList.addAll(mcCouponNOList);

            //如果最终的营销活动信息非空
            if (!mcInfoList.isEmpty()) {
                List<BankToClientMcStatInfoBean> bankToClientMcStatInfoListResult = new ArrayList<>();
                //循环遍历上步查询到的营销活动
                for (StatisticsStatementQueryRespInfo statisticsStatementQueryRespInfo : mcInfoList) {
                    if (!StringUtil.isNullorEmpty(statisticsStatementQueryRespInfo.getInitStatData())) {
                        initStatData = statisticsStatementQueryRespInfo.getInitStatData();
                    }
                    //查询对客营销活动统计信息
                    List<TMStat2C> statToClientList = new ArrayList<>();
                    getAllStatToClientInfo(queryBankToClientMcStatRequest, statisticsStatementQueryRespInfo.getMcNo(), statToClientList);
                    //如果对客营销活动统计表数据查询不为空
                    if (!statToClientList.isEmpty()) {
                        //将表数据转换为响应实体对象
                        List<BankToClientMcStatInfoBean> bankToClientMcStatInfoList = statToClientList.stream().map(this::convertBankToClientMcStatInfoBean).collect(Collectors.toList());

                        //循环遍历对客营销活动统计信息
                        for (BankToClientMcStatInfoBean bankToClientMcStatInfo : bankToClientMcStatInfoList) {
                            List<TMStat2CDay> statToClientDayList = new ArrayList<>();
                            //查询对客营销活动单日统计信息
                            getAllStatToClientDayInfo(queryBankToClientMcStatRequest, statisticsStatementQueryRespInfo.getMcNo(), statToClientDayList);
                            if (!statToClientDayList.isEmpty()) {
                                //将表数据转换为响应实体对象
                                List<BankToClientMcTxnStatInfoBean> bankToClientMcTxnStatInfoList = statToClientDayList.stream().map(this::convertBankToClientDayMcStatInfo).collect(Collectors.toList());
                                bankToClientMcStatInfo.setTxnStatInfoList(bankToClientMcTxnStatInfoList);
                            }
                            bankToClientMcStatInfoListResult.add(bankToClientMcStatInfo);
                        }
                    }
                }
                queryBankToClientMcStatResponse.setTotalRows(String.valueOf(bankToClientMcStatInfoListResult.size()));
                if (queryBankToClientMcStatRequest.getPageIndex().equals("-1") || queryBankToClientMcStatRequest.getPageSize().equals("-1")) {
                    queryBankToClientMcStatResponse.setTotalPage(CommonConstant.ONE_COMMON_CONSTANT);
                    queryBankToClientMcStatResponse.setPageIndex(CommonConstant.ONE_COMMON_CONSTANT);
                    queryBankToClientMcStatResponse.setPageSize(String.valueOf(bankToClientMcStatInfoListResult.size()));
                    //查询到的链表信息
                    queryBankToClientMcStatResponse.setMcStatInfoList(bankToClientMcStatInfoListResult);
                } else {
                    if (0 == (bankToClientMcStatInfoListResult.size() % Integer.parseInt(queryBankToClientMcStatRequest.getPageSize()))) {
                        queryBankToClientMcStatResponse.setTotalPage(String.valueOf(bankToClientMcStatInfoListResult.size() / Math.abs(Integer.parseInt(queryBankToClientMcStatRequest.getPageSize()))));
                    } else {
                        queryBankToClientMcStatResponse.setTotalPage(String.valueOf(bankToClientMcStatInfoListResult.size() / Math.abs(Integer.parseInt(queryBankToClientMcStatRequest.getPageSize())) + 1));
                    }

                    queryBankToClientMcStatResponse.setPageIndex(queryBankToClientMcStatRequest.getPageIndex());
                    queryBankToClientMcStatResponse.setPageSize(queryBankToClientMcStatRequest.getPageSize());
                    //按照分页返回查询到的数据
                    /* 分页查询 */
                    int fromIndex = Integer.parseInt(queryBankToClientMcStatRequest.getPageIndex()) * Integer.parseInt(queryBankToClientMcStatRequest.getPageSize());
                    int endIndex = fromIndex + Integer.parseInt(queryBankToClientMcStatRequest.getPageSize());
                    if (fromIndex <= bankToClientMcStatInfoListResult.size()) {
                        List<BankToClientMcStatInfoBean> subMcMoneyOffInfoList = bankToClientMcStatInfoListResult.subList(fromIndex, Math.min(endIndex, bankToClientMcStatInfoListResult.size()));
                        queryBankToClientMcStatResponse.setMcStatInfoList(subMcMoneyOffInfoList);
                    }
                }

            } else {
                rglog.error("根据查询条件<{}>查询到营销活动信息,通过机构筛选后无营销活动信息!", queryClause);
                queryBankToClientMcStatResponse.setTotalPage(CommonConstant.ZERO_COMMON_CONSTANT);
                queryBankToClientMcStatResponse.setTotalRows(CommonConstant.ZERO_COMMON_CONSTANT);
            }
        }
        bizResponse.setRspSysId(ChannelEnum.MOMP.getChannelType());
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        queryBankToClientMcStatResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        queryBankToClientMcStatResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());

        bizResponse.setResult(queryBankToClientMcStatResponse);

        return bizResponse;
    }

    /**
     * @param queryBankToClientMcStatRequest
     * @param organizationInfoList
     * @return void
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/4/14
     * @Description: 查询所属机构及下属机构信息
     */
    private void getOrganizationInfoList(QueryBankToClientMcStatRequest queryBankToClientMcStatRequest, List<Organization> organizationInfoList) {

        String queryClause = queryBankToClientMcStatRequest.getHeader().getInstId() + queryBankToClientMcStatRequest.getQueryOrgId();
        String orgCode = queryBankToClientMcStatRequest.getQueryOrgId();
        rglog.error("接口请求上送的查询主体所属机构为<{}>", orgCode);
        try {
            StatisticsStatementInfoMapper statisticsStatementInfoMapper = new StatisticsStatementInfoMapperImpl();

            //查询主体机构编码级别
            List<Organization> objectList = statisticsStatementInfoMapper.selectOrganization(queryBankToClientMcStatRequest.getHeader().getInstId(),
                    orgCode, null);

            if (null == objectList) {
                rglog.error("根据查询条件<{}>查询主体所属机构信息失败!", queryClause);
                throw new BizException(RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespDesc());
            }
            //所有的机构集合，包含总行，分行，支行
            //将接口查询主体所属机构加入
            organizationInfoList.addAll(objectList);
            //获取机构编码级别
            String orgLevel = objectList.get(0).getOrgLevel();
            //如果是总行级别，需要获取分行和支行
            if (CommonConstant.ORGLEVEL_BANK.equals(orgLevel)) {
                List<Organization> branchObjectList = statisticsStatementInfoMapper.selectOrganization(queryBankToClientMcStatRequest.getHeader().getInstId(),
                        null, orgCode);
                if (null != branchObjectList) {
                    //将分行信息加入
                    organizationInfoList.addAll(branchObjectList);
                    //遍历获取支行信息
                    for (Organization branchObject : branchObjectList) {
                        List<Organization> subbranchObjectList = statisticsStatementInfoMapper.selectOrganization(queryBankToClientMcStatRequest.getHeader().getInstId(),
                                null, branchObject.getOrgCode());
                        //将支行信息加入
                        organizationInfoList.addAll(subbranchObjectList);
                    }
                }
            } else if (CommonConstant.ORGLEVEL_BANK_BRANCH.equals(orgLevel)) {
                List<Organization> subbranchObjectList = statisticsStatementInfoMapper.selectOrganization(queryBankToClientMcStatRequest.getHeader().getInstId(),
                        null, orgCode);
                //将支行信息加入
                organizationInfoList.addAll(subbranchObjectList);
            } else {
                //扫描添加的空
            }
        } catch (Exception e) {
            rglog.error("查询主体所属机构信息失败!");
            throw new BizException(RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespCode(), RespCodeEnum.GET_MC_ORG_INFO_ERROR.getRespDesc());
        }
    }

    /**
     * @param queryBankToClientMcStatRequest
     * @param mcMoneyOffList
     * @return void
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/4/12
     * @Description: 查询减免类营销活动信息
     */
    private void getMcMoneyOffListInfo(QueryBankToClientMcStatRequest queryBankToClientMcStatRequest, List<StatisticsStatementQueryRespInfo> mcMoneyOffList) {

        String queryClause = queryBankToClientMcStatRequest.getHeader().getInstId() + queryBankToClientMcStatRequest.getMcNo()
                + queryBankToClientMcStatRequest.getMcName() + queryBankToClientMcStatRequest.getMcType()
                + queryBankToClientMcStatRequest.getMcStatus() + queryBankToClientMcStatRequest.getQueryOrgId();
        try {
            StatisticsStatementInfoMapper statisticsStatementInfoMapper = new StatisticsStatementInfoMapperImpl();
            List<StatisticsStatementQueryRespInfo> objectList = statisticsStatementInfoMapper.selectMcMoneyOffListInfo(queryBankToClientMcStatRequest.getHeader().getInstId(),
                    queryBankToClientMcStatRequest.getMcNo(),
                    queryBankToClientMcStatRequest.getMcName(),
                    queryBankToClientMcStatRequest.getMcType(),
                    queryBankToClientMcStatRequest.getMcStatus(),
                    queryBankToClientMcStatRequest.getQueryOrgId());
            if (null != objectList) {
                mcMoneyOffList.addAll(objectList);
            }
        } catch (Exception e) {
            rglog.error("根据查询条件<{}>获取减免类营销活动列表信息失败!", queryClause);
            throw new BizException(RespCodeEnum.GET_MONEY_OFF_MC_LIST_ERROR.getRespCode(), RespCodeEnum.GET_MONEY_OFF_MC_LIST_ERROR.getRespDesc());
        }
    }

    /**
     * @param queryBankToClientMcStatRequest
     * @param mcCouponList
     * @return void
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/4/12
     * @Description: 查询卡券类营销活动信息
     */
    private void getMcCouponListInfo(QueryBankToClientMcStatRequest queryBankToClientMcStatRequest, List<StatisticsStatementQueryRespInfo> mcCouponList) {

        String queryClause = queryBankToClientMcStatRequest.getHeader().getInstId() + queryBankToClientMcStatRequest.getMcNo()
                + queryBankToClientMcStatRequest.getMcName() + queryBankToClientMcStatRequest.getMcType()
                + queryBankToClientMcStatRequest.getMcStatus() + queryBankToClientMcStatRequest.getQueryOrgId();
        try {
            StatisticsStatementInfoMapper statisticsStatementInfoMapper = new StatisticsStatementInfoMapperImpl();
            List<StatisticsStatementQueryRespInfo> objectList = statisticsStatementInfoMapper.selectMcCouponListInfo(queryBankToClientMcStatRequest.getHeader().getInstId(),
                    queryBankToClientMcStatRequest.getMcNo(),
                    queryBankToClientMcStatRequest.getMcName(),
                    queryBankToClientMcStatRequest.getMcType(),
                    queryBankToClientMcStatRequest.getMcStatus(),
                    queryBankToClientMcStatRequest.getQueryOrgId());
            if (null != objectList) {
                mcCouponList.addAll(objectList);
            }
        } catch (Exception e) {
            rglog.error("根据查询条件<{}>获取卡券类营销活动列表信息失败!", queryClause);
            throw new BizException(RespCodeEnum.GET_COUPON_MC_LIST_ERROR.getRespCode(), RespCodeEnum.GET_COUPON_MC_LIST_ERROR.getRespDesc());
        }
    }

    /**
     * @param queryBankToClientMcStatRequest
     * @param statToClientList
     * @return void
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/4/1
     * @Description: 查询对客营销活动统计信息
     */
    private void getAllStatToClientInfo(QueryBankToClientMcStatRequest queryBankToClientMcStatRequest, String mcNo, List<TMStat2C> statToClientList) {

        String queryClause = queryBankToClientMcStatRequest.getHeader().getInstId() + mcNo;
        try {
            StatisticsStatementInfoMapper statisticsStatementInfoMapper = new StatisticsStatementInfoMapperImpl();
            List<TMStat2C> objectList = statisticsStatementInfoMapper.selectStatToClientListInfo(queryBankToClientMcStatRequest.getHeader().getInstId(),
                    mcNo);
            if (null != objectList) {
                statToClientList.addAll(objectList);
            }
        } catch (Exception e) {
            rglog.error("根据查询条件<{}>获取对客户营销活动统计信息失败!", queryClause);
            throw new BizException(RespCodeEnum.GET_MONEY_OFF_MC_LIST_ERROR.getRespCode(), RespCodeEnum.GET_MONEY_OFF_MC_LIST_ERROR.getRespDesc());
        }
    }

    /**
     * @param queryBankToClientMcStatRequest
     * @param statToClientDayList
     * @return void
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/4/1
     * @Description: 查询对客营销活动单日统计信息
     */
    private void getAllStatToClientDayInfo(QueryBankToClientMcStatRequest queryBankToClientMcStatRequest, String mcNo, List<TMStat2CDay> statToClientDayList) {
        //获取当前日期
        String nowDate = DateUtil.getCurrentDate();
        String queryClause = queryBankToClientMcStatRequest.getHeader().getInstId() + mcNo + nowDate;
        try {
            StatisticsStatementInfoMapper statisticsStatementInfoMapper = new StatisticsStatementInfoMapperImpl();
            List<TMStat2CDay> objectList = statisticsStatementInfoMapper.selectStatToClientDayListInfo(queryBankToClientMcStatRequest.getHeader().getInstId(),
                    mcNo, nowDate);
            if (null != objectList) {
                statToClientDayList.addAll(objectList);
            }
        } catch (Exception e) {
            rglog.error("根据查询条件<{}>获取对客户营销活动单日统计信息失败!", queryClause);
            throw new BizException(RespCodeEnum.GET_MONEY_OFF_MC_LIST_ERROR.getRespCode(), RespCodeEnum.GET_MONEY_OFF_MC_LIST_ERROR.getRespDesc());
        }
    }

    /**
     * @return cc.rengu.igas.momp.facade.bean.BankToClientMcStatInfoBean
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/4/1
     * @Description: 对客营销活动统计表转换为为营销活动统计信息对象列表
     */
    private BankToClientMcStatInfoBean convertBankToClientMcStatInfoBean(TMStat2C statToClient) {
        BankToClientMcStatInfoBean bankToClientMcStatInfo = new BankToClientMcStatInfoBean();
        try {
            //营销活动编号
            bankToClientMcStatInfo.setMcNo(statToClient.getMcNo());
            //总预算累计消耗笔数
            bankToClientMcStatInfo.setTotalQtyAcc(statToClient.getTotalQtyAcc());
            //总预算累计消耗金额
            bankToClientMcStatInfo.setTotalAmtAcc(statToClient.getTotalAmtAcc());
            //银行预算累计消耗笔数
            bankToClientMcStatInfo.setBankQtyAcc(statToClient.getBankQtyAcc());
            //银行预算累计消耗金额
            bankToClientMcStatInfo.setBankAmtAcc(statToClient.getBankAmtAcc());
            //商户预算累计消耗笔数
            bankToClientMcStatInfo.setMchntQtuAcc(statToClient.getMchntQtyAcc());
            //商户预算累计消耗金额
            bankToClientMcStatInfo.setMchntAmtAcc(statToClient.getMchntAmtAcc());
            //累计参与商户数
            bankToClientMcStatInfo.setMchntQty(statToClient.getMchntQty());
            //曝光次数
            bankToClientMcStatInfo.setExposureQty(statToClient.getExposureQty());
            //曝光人数
            bankToClientMcStatInfo.setExposurePeopleQty(statToClient.getExposurePeopleQty());
            //总预算笔均消耗金额
            bankToClientMcStatInfo.setAvgTotalAmt(statToClient.getAvgTotalAmt());
            //银行预算笔均消耗金额
            bankToClientMcStatInfo.setAvgBankAmt(statToClient.getAvgBankAmt());
            //商户预算笔均消耗金额
            bankToClientMcStatInfo.setAvgMchntAmt(statToClient.getAvgMchntAmt());
            //日均总预算笔均消耗金额
            bankToClientMcStatInfo.setDayAvgTotalAmt(statToClient.getDayAvgTotalAmt());
            //日均银行预算笔均消耗金额
            bankToClientMcStatInfo.setDayAvgBankAmt(statToClient.getDayAvgBankAmt());
            //日均商户预算笔均消耗金额
            bankToClientMcStatInfo.setDayAvgMchntAmt(statToClient.getDayAvgMchntAmt());
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换对客营销活动统计信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        return bankToClientMcStatInfo;
    }

    /**
     * @return cc.rengu.igas.momp.facade.bean.BankToClientMcTxnStatInfoBean
     * @throws Exception 异常信息
     * @author liujinan
     * @date 2020/4/1
     * @Description: 对客营销活动单日统计表转换为为营销活动单日统计信息对象列表
     */
    private BankToClientMcTxnStatInfoBean convertBankToClientDayMcStatInfo(TMStat2CDay statToClientDay) {
        BankToClientMcTxnStatInfoBean bankToClientMcTxnStatInfo = new BankToClientMcTxnStatInfoBean();
        try {

            //统计表中没有的字段,赋值为0
            //交易日期
            bankToClientMcTxnStatInfo.setTxnDate(statToClientDay.getTxnDate());
            //活动期间商户交易笔数
            if (!StringUtil.isNullorEmpty(statToClientDay.getTotalQtyAcc())) {
                bankToClientMcTxnStatInfo.setMcMchntQty(statToClientDay.getTotalQtyAcc());
            } else {
                bankToClientMcTxnStatInfo.setMcMchntQty("0");
            }
            //原所有商户日均交易笔数
            if (!StringUtil.isNullorEmpty(initStatData) && !initStatData.equals(CommonConstant.ZERO_COMMON_CONSTANT)) {
                bankToClientMcTxnStatInfo.setOriginalMchntQty(JSON.parseObject(initStatData).getString("dayAvgCount"));
            } else {
                bankToClientMcTxnStatInfo.setOriginalMchntQty("0");
            }
            //活动期间商户交易金额-累计交易金额
            if (!StringUtil.isNullorEmpty(statToClientDay.getTxnQtyAcc())) {
                bankToClientMcTxnStatInfo.setMcMchntAmt(statToClientDay.getTxnQtyAcc());
            } else {
                bankToClientMcTxnStatInfo.setMcMchntAmt("0.00");
            }
            //原所有商户日均交易金额
            if (!StringUtil.isNullorEmpty(initStatData) && !initStatData.equals(CommonConstant.ZERO_COMMON_CONSTANT)) {
                bankToClientMcTxnStatInfo.setOriginalMchntAmt(JSON.parseObject(initStatData).getString("dayAvgAmount"));
            } else {
                bankToClientMcTxnStatInfo.setOriginalMchntAmt("0.00");
            }
        } catch (Exception e) {
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            e.printStackTrace(new PrintWriter(byteArrayOutputStream, true));
            rglog.error("转换对客营销活动单日统计信息异常,异常信息:<{}>", byteArrayOutputStream.toString());
            throw new BizException(RespCodeEnum.SYSTEM_ERROR.getRespCode(), RespCodeEnum.SYSTEM_ERROR.getRespDesc());
        }
        return bankToClientMcTxnStatInfo;
    }
}
